package com.llmm.waterMark;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @description: 图片水印
 * @author: llmm
 * @date: 2022/2/26
 */
public class ImageWaterMark {

    public static void main(String[] args) {
        //原图
        BufferedImage image = null;
        //水印标记
        BufferedImage waterImage =null;
        try {
            image = ImageIO.read(new File("E:\\sister.jpg"));
            waterImage = ImageIO.read(new File("E:\\202107.jpg"));
            new ImageWaterMark().addImageWaterMark(image, waterImage, "E:\\sisterImag.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addImageWaterMark(BufferedImage targetImg, BufferedImage waterImg, String outPath) {
        try {
            int width = targetImg.getWidth(); //图片宽
            int height = targetImg.getHeight(); //图片高
            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(targetImg, 0, 0, width, height, null);
            // 水印内容放置在右下角
            int x = width - waterImg.getWidth();
            int y = height- waterImg.getHeight();
            g.drawImage(waterImg, x, y, waterImg.getWidth(), waterImg.getHeight(), null);
            FileOutputStream outImgStream = new FileOutputStream(outPath);
            ImageIO.write(bufferedImage, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
